<html>
<title>1D Rezný plán</title>
<body>
  <form name="1d_cutting_plan_form" action="solver">
    <div>Číslo zákazky: <input type="text" name="order_num" value=""/></div>
    <div id="order_num_err" style="color:red;float:left;"></div>
    <br /><br />    
    <div>Typ profilu: <input type="text" name="stock_type" value=""/></div>
    <div id="stock_type_err" style="color:red;float:left;"></div>
    <br /><br />  
    <div>Štandarná dĺžka polotovaru: <input type="text" name="stock_length" value="6000" size="6"/></div>
    <div id="stock_length_err" style="color:red;float:left;"></div>
    <br /><br />
    <div>Hrúbka rezu (0-15): <input type="text" name="cut_thickness" value="0" size="2"/></div>
    <div id="cut_thickness_err" style="color:red;float:left;"></div>
    <br /><br />    
    <div>Prírezové dľžky (oddelené medzerou alebo čiarkou):<br />
      <textarea cols="37" rows="5" name="custom_lengths"></textarea>
    </div>
    <div id="custom_lengths_err" style="color:red;float:left;"></div>
    <br /><br />
    <input type="submit" value="Rezný plán" onclick="return validateForm();" />  
  </form>
  <script type="text/javascript">
    var MAX_CUSTOM_LENGTHS_NUM = 60;
    
    function validateForm() {
        var isOrderNumValid = true;
        var isStockTypeValid = true;
        var isStockLengthValid = true;
        var isCutThicknessValid = true;
        var isCustomLengthValid = true;
        
        var orderNum = document.forms["1d_cutting_plan_form"]["order_num"].value;
        if (orderNum == "") {
            document.getElementById('order_num_err').innerHTML = 'Chýba číslo zákazky !';            
            isOrderNumValid = false;
        }                
        if (isOrderNumValid) {     
            document.getElementById('order_num_err').innerHTML = '';
        }                

        var stockType = document.forms["1d_cutting_plan_form"]["stock_type"].value;
        if (stockType == "") {
            document.getElementById('stock_type_err').innerHTML = 'Chýba typ profilu !';            
            isStockTypeValid = false;
        }
        if (isStockTypeValid) {     
            document.getElementById('stock_type_err').innerHTML = '';
        }                

        var stockLength = document.forms["1d_cutting_plan_form"]["stock_length"].value;        
        if (isNaN(stockLength)) {
            document.getElementById('stock_length_err').innerHTML = 'Nesprávne zadaná štandarná dĺžka !';            
            isStockLenghtValid = false;
        }
        if (stockLength == "") {
            document.getElementById('stock_length_err').innerHTML = 'Chýba štandarná dĺžka !';            
            isStockLengthValid = false;
        }
        if (!isNaN(stockLength) && (parseInt(stockLength) < 0 || parseInt(stockLength) > 50000))  {
            document.getElementById('stock_length_err').innerHTML = 'Štandarná dĺžka nesmie byť menšia ako 0 a väčšia ako 50000 !';            
            isStockLengthValid = false;
        }           
        if (isStockLengthValid) {     
            document.getElementById('stock_length_err').innerHTML = '';
        }
        
        var cutThickness = document.forms["1d_cutting_plan_form"]["cut_thickness"].value;        
        if (isNaN(cutThickness)) {
            document.getElementById('cut_thickness_err').innerHTML = 'Nesprávne zadaná hrúbka rezu !';            
            isCutThicknessValid = false;
        }
        if (!isNaN(cutThickness) && (parseInt(cutThickness) < 0 || parseInt(cutThickness) > 15))  {
            document.getElementById('cut_thickness_err').innerHTML = 'Hrúbka rezu nesmie byť menšia ako 0 a väčšia ako 15 !';            
            isCutThicknessValid = false;
        }        
        if (cutThickness == "") {
            document.forms["1d_cutting_plan_form"]["cut_thickness"].value = '0';            
        }        
        if (isCutThicknessValid) {
            document.getElementById('cut_thickness_err').innerHTML = '';
        }
        
        var customLengths = document.forms["1d_cutting_plan_form"]["custom_lengths"].value.replace(/,/g, " ").split(" ");
        var c = 1;
        for(i = 0; i < customLengths.length; i++) {
            if (customLengths[i] == "") {
                continue;
            }
            if (isNaN(customLengths[i])) {
                document.getElementById('custom_lengths_err').innerHTML = 'Nesprávne zadaná prírezová dĺžka !';
                isCustomLengthValid = false;
                break;
            }
            if (parseInt(customLengths[i]) > parseInt(stockLength) || parseInt(customLengths[i]) < 0) {
                document.getElementById('custom_lengths_err').innerHTML = 'Prírezová dĺžka nesmie byť menšia ako 0 a väčšia ako ' + stockLength + ' !';
                isCustomLengthValid = false;
                break;
            } 
            if (c > MAX_CUSTOM_LENGTHS_NUM) {
                document.getElementById('custom_lengths_err').innerHTML = 'Počet prírezových dĺžok nesmie prekročiť ' + MAX_CUSTOM_LENGTHS_NUM + ' ! (Bolo zadaných ' + c + ')';
                isCustomLengthValid = false;
            }    
            c++;     
        }             
        if (customLengths == "") {
            document.getElementById('custom_lengths_err').innerHTML = 'Chýba prírezová dĺžka !';
            isCustomLengthValid = false;         
        }                               
        if (isCustomLengthValid) {
            document.getElementById('custom_lengths_err').innerHTML = '';
        }
                                      
        return (isOrderNumValid && isStockTypeValid && isStockLengthValid && isCutThicknessValid && isCustomLengthValid);
    }
  </script>
</body>
</html>
